<template>
  <div class="login-main">
    <div @click="comeBack('/')" class="login-header">
      <van-icon name="arrow-left" />
      <img src="@/assets/image/lang.png" alt="">
    </div>
    <div class="title">注册</div>
    <div class="login-method">
      <!--      <div class="box">手机号</div>-->
<!--      <div class="box active">邮箱</div>-->
    </div>
    <div class="form">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model.number="ruleForm.email"  placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item style="margin-top: -20px" prop="name">
          <template slot="label">
            <span class="box-title">手机号</span>
          </template>
          <div class="box-input">
            <el-input placeholder="请输入手机号" v-model="ruleForm.whatsApp" class="input-with-select">
              <div slot="prepend" @click="chekckCountry">
                <span>+44</span>
                <van-icon name="arrow-down"/>
              </div>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input show-password type="password" v-model="ruleForm.pass"  placeholder="请输入密码" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input show-password type="password"  placeholder="请确认密码" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <div @click="submitForm('ruleForm')"  class="submit">注册</div>
        </el-form-item>
      </el-form>

    </div>
    <div class="form-tips">
      还没账号？<a @click="toLogin" href="javascript:void (0)">去登录</a>
    </div>

    <van-popup closeable round v-model="openCountry" position="bottom" :style="{ height: '80%' }">
      <div class="content-main">
        <div class="title">选择区域码</div>
        <div class="input">
          <div class="input-label">
            <img src="@/assets/image/search-b.png" alt="">
            <input type="text" placeholder="请输入要搜索的国家名称">
          </div>
          <div class="country-list">
            <div class="item">
                <span class="left">
                  <img src="@/assets/image/zh.png" alt="">
                  <span>中国</span>
                </span>
              <span class="right">+86</span>
            </div>
            <div class="item">
                <span class="left">
                  <img src="@/assets/image/zh.png" alt="">
                  <span>中国</span>
                </span>
              <span class="right">+86</span>
            </div>
            <div class="item">
                <span class="left">
                  <img src="@/assets/image/zh.png" alt="">
                  <span>中国</span>
                </span>
              <span class="right">+86</span>
            </div>
            <div class="item">
                <span class="left">
                  <img src="@/assets/image/zh.png" alt="">
                  <span>中国</span>
                </span>
              <span class="right">+86</span>
            </div>
          </div>
        </div>
      </div>
    </van-popup>

  </div>
</template>

<script>
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('邮箱不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value < 18) {
            callback(new Error('必须年满18岁'));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      openCountry:false,
      ruleForm: {
        pass: '',
        checkPass: '',
        email: '',
        whatsApp:''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        email: [
          { validator: checkAge, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    toLogin(){
      this.$router.push('/login')
    },
    comeBack(item){
      this.$router.push(item)
    },
    chekckCountry() {
      this.openCountry = true
    }
  }
}
</script>


<style scoped lang="scss">
.login-main{
  width: 100%;
  padding: 3% 4%;
  box-sizing: border-box;
  .login-header{
    width: 100%;
    height: 40px;
    background: #fff;
    font-size: 24px;
    display: flex;
    justify-content: space-between;

    box-sizing: border-box;
    img{
      height: 22px;
    }
  }
  .title{
    width: 100%;
    margin-top: 10px;
    font-weight: 600;
    font-size: 28px;
  }
  .login-method{
    width: 100%;
    margin-top: 20px;
    display: flex;
    .box{
      padding: 8px 30px;
      background: #eee;
      font-style: normal;
      font-weight: 400;
      font-size: 14px;
      color: #333;
      border-radius: 5px;
      margin-right: 15px;
    }
    .active{
      background: #002FFF;
      color: #fff;
    }
  }
  .form{
    width: 100%;
    margin-top: 20px;
    margin-bottom: 20px;
    .submit{
      width: 100%;
      display: block;
      justify-content: center;
      align-items: center;
      font-size: 16px;
      text-align: center;
      color: #fff;
      background: #002FFF;
      border-radius: 5px;
    }
  }
  .form-tips{
    width: 100%;
    font-size: 14px;
    text-align: center;
    a{
      font-size: 14px;
      color: #4aa8ff;
    }
  }
}
.content-main {
  width: 100%;

  .title {
    width: 100%;
    text-align: center;
    line-height: 50px;
    font-size: 14px;
  }

  .input {
    width: 100%;
    height: 30px;
    padding: 0 10px;
    box-sizing: border-box;

    .input-label {
      width: 100%;
      height: 30px;
      background: #f6f6f6;
      display: flex;
      align-items: center;
      padding: 0 3%;
      box-sizing: border-box;
      font-size: 12px;

      img {
        margin-right: 10px;
        height: 14px;
      }
    }

    .country-list {
      width: 100%;
      padding: 0 3%;
      box-sizing: border-box;
      margin-top: 30px;

      .item {
        width: 100%;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 15px;

        .left {
          color: #333333;
          font-size: 12px;
          display: flex;
          align-items: center;

          img {
            margin-right: 10px;
            height: 18px;
          }
        }

        .right {
          font-size: 14px;
          color: #666;
        }
      }
    }
  }
}
</style>